home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7919 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.2 KB  |  49 lines

  1. Path: columba.udac.uu.se!news
  2. From: Enrico Savazzi <enrico.savazzi@pal.uu.se>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Run time dynamic 2-D array?
  5. Date: Wed, 14 Feb 1996 15:59:24 +0100
  6. Organization: Uppsala University
  7. Message-ID: <3121F8CC.132F@pal.uu.se>
  8. References: <4flcq9$i1k@vixen.cso.uiuc.edu>
  9. NNTP-Posting-Host: esavazzi.pal.uu.se
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (WinNT; I)
  14.  
  15. WEIMIN YANG wrote:
  16. > I need to use a 2-D array. I use following code.
  17. > void tryit(int a, int b) {
  18. > float c= new float[b][a];
  19. > ...
  20. > ....
  21. > }
  22. > I get compile error. Is there another way to do it? By the way, I don't want to
  23. > use 1-D array to implement it.
  24.  
  25. Do this:
  26.  
  27. float** c;
  28. c = new float* [b];
  29. for (int i = 0; i < b; i++)
  30.     c[i] = new float [a];
  31. ....
  32. for (int i = 0; i < b; i++)
  33.     delete [] c[i];
  34. delete c;
  35.  
  36. It is surprising how often this question comes up, and how 
  37. little many C++ books say about it. When I had to tackle this 
  38. problem for the first time, I had to use a uni-dimensional 
  39. array, and found the solution by chance only a few months later.
  40. Uni-dimensional arrays are faster and contiguous, however, so 
  41. you might still consider using one.
  42.